package com.comdosoft.comdoandroidbase.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.comdosoft.comdoandroidbase.R;

import java.util.List;


/**
 * 只显示文字的通用BaseAdapter
 * 
 * @author niemin
 * 
 */
public class SelectAdapter<T> extends BaseAdapter {

	private List<T> dictIndustries;

	private LayoutInflater inflater;
	private Context mContext;
	private int selectPosition = -1;

	public int getSelectPosition() {
		return selectPosition;
	}

	public void setSelectPosition(int selectPosition) {
		this.selectPosition = selectPosition;
	}

	public SelectAdapter(Context context, List<T> dictIndustries) {
		this.dictIndustries = dictIndustries;
		this.mContext = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return dictIndustries.size();
	}

	@Override
	public T getItem(int position) {
		return dictIndustries.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;

		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_list,parent, false);
			viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.nameTextViews);
			viewHolder.type_pop = (RelativeLayout) convertView.findViewById(R.id.type_pop);
			convertView.setTag(viewHolder);
		}

		viewHolder = (ViewHolder) convertView.getTag();

		/*
		 * 和这个人的关系
		 */
		//		else if (dictIndustries.get(0) instanceof DictUserRelation) {
		//			viewHolder.nameTextView.setText(((DictUserRelation) dictIndustries
		//					.get(position)).getName());
		//		}
		if (dictIndustries.get(0) instanceof String) {
			viewHolder.nameTextView.setText(((String) dictIndustries
					.get(position)));
			if (selectPosition == position) {
				viewHolder.type_pop.setBackgroundResource(R.color.materialBlue);
				viewHolder.nameTextView.setTextColor(mContext.getResources().getColor(R.color.textColorPrimary_dark));
			} else {
				viewHolder.type_pop.setBackgroundResource(R.color.textColorPrimary);
				viewHolder.nameTextView.setTextColor(mContext.getResources().getColor(R.color.textColorPrimary_dark));
			}
		}
		return convertView;
	}

	static class ViewHolder {

		TextView nameTextView;
		RelativeLayout type_pop;

	}
}
